<?php
namespace Home\Controller\Goods;
use Home\Controller\Layout\LayoutController;
class CartController extends LayoutController {
	public function index(){
		if(empty(session('home_id'))){
			echo '<script>alert("请先登录");window.location.href="'.U('/home/login/login/login').'"</script>';
			return ;
		}
		$data = self::cart_data();
		$this->list=$data;
		$this->display();
	}
	public static function cart_data(){
		$where['a.uid']=session('home_id');
		$where['a.status']=0;
		$data = M('g_cart')->alias('a')
							->field(array('a.*,b.goods_name,b.goods_price,b.goods_stock,b.pinpai'))
							->join('left join think_g_goods b on a.gid=b.id')
							->where($where)
							->select();							
		$pic = M('g_goods_pic')->select();
		foreach($data as &$v){
			foreach($pic as $val){
				if($v['gid']==$val['gid'] && $val['goods_zpic']=='1'){
					$v['pic']=$val['goods_pic'];
					$v['subtotal']=$v['num']*$v['goods_price'];
				}
			}
		}
		return $data;
	}
	public function insert(){
		if(empty(session('home_id'))){
			echo 'alert("您还没有登录,请登录后在添加商品!");location.href="/home/login/login/login"';
			return ;
		}
		$cart = M('g_cart');
		$data = I('post.');
		$data['uid']=session('home_id');
		$data['time']=time();
		$data['num']=1;
		$data['status']=0;
		$where['uid']=$data['uid'];
		$where['gid']=$data['gid'];
		$where['status']=0;
		$num = $cart->where($where)->find();
		if($num['num'] >= M('g_goods')->where('id='.$data['gid'])->find()['goods_stock']){
			echo 'chaochu';
			return ;
		}
		if($num){
			$data['num']=$num['num']+1;
			$data['id']=$num['id'];
			$cart->create($data);
			if($cart->save()){
				echo 'yes';
			}else{
				echo 'no';
			}
		}else{
			$cart->create($data);
			if($cart->add()){
				echo 'yes';
			}else{
				echo 'no';
			}
		}
	}
	public function add(){
		if(empty(session('home_id'))){
			echo '<script>alert("您还没有登录,请登录后在添加商品!");location.href="/home/login/login/login"</script>';
			return ;
		}
		$cart = M('g_cart');
		$data = I('post.');
		$data['uid']=session('home_id');
		$data['status']=0;
		$data['time']=time();
		$where['uid']=$data['uid'];
		$where['gid']=$data['gid'];
		$where['status']=0;
		$num = $cart->where($where)->find();
		if(($num['num']+$data['num']) > M('g_goods')->where('id='.$data['gid'])->find()['goods_stock']){
			echo "<script>alert('添加失败,货源不足');location.href='/home/goods/detail/index/id/".$data['gid']."'</script>";
			return ;
		}
		if($num){
			$data['num']=$num['num']+$data['num'];
			$data['id']=$num['id'];
			$cart->create($data);
			if($cart->save()){
				echo "<script>location.href='/home/goods/cart/index'</script>";
			}else{
				echo "<script>alert('添加失败,请刷新页面之后在重新添加');location.href='/home/goods/detail/index/id/".$data['gid']."'</script>";
			}
		}else{
			$cart->create($data);
			if($cart->add()){
				echo "<script>location.href='/home/goods/cart/index'</script>";
			}else{
				echo "<script>alert('添加失败,请刷新页面之后在重新添加');location.href='/home/goods/detail/index/id/".$data['gid']."'</script>";
			}
		}
	}
	public function update(){
		// var_dump(I('post.'));
		$cart = M('g_cart');
		$cart->create();
		if($cart->save()){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function del(){
		if(M('g_cart')->where('id='.I('post.id'))->save(['status'=>'1'])){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function sess(){
		// echo 'aaa';
		// var_dump(I('post.'));
		session('cart_id',I('post.cart_id'));
		if(session('cart_id')){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	 public function count_cart(){
        if(session('home_id')){
            $where['uid']=session('home_id');
            $where['status']=0;
            echo M('g_cart')->where($where)->count();
        }
    }
}